package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> orderSettings) {
        for (OrderSetting orderSetting : orderSettings) {

            //判断当前的日期之前是否已经设置过预约日期，使用当前日期作为查询条件
            long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
            if (count>0){
                //如果之前已经设置过就是不再重复添加而是进行数据更新
                orderSettingDao.editNumberByOrderDate(orderSetting);
            }else{
                //如果当前日期之前没有设置过预约，就添加保存
                orderSettingDao.add(orderSetting);
            }
        }
    }

    /**  传递的参数
     *   date（格式：2019-8）
     *  构建的数据List<Map>
     *    map.put(“date”,1);
     map.put(“number”,120);
     map.put(“reservations”,10);

     *  查询方案：SELECT * FROM t_ordersetting WHERE orderDate LIKE '2019-08-%'
     *  查询方案：SELECT * FROM t_ordersetting WHERE orderDate BETWEEN '2019-9-1' AND '2019-9-31'
     */
//根据日期查询预约设置数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        // 1.组织查询Map，dateBegin表示月份开始时间，dateEnd月份结束时间
        String dateBegin = date + "-1";//2019-3-1
        String dateEnd = date + "-31";//2019-3-31
        Map<String,Object>  map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        // 2.查询当前月份的预约设置
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        // 3.将List<OrderSetting>，组织成List<Map>
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断当前的日期之前是否已经设置过预约日期，使用当前日期作为查询条件
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count>0){
            //如果之前已经设置过就是不再重复添加而是进行数据更新
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //如果当前日期之前没有设置过预约，就添加保存
            orderSettingDao.add(orderSetting);
        }
    }

}
